﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LDSP.Helper;
using LDSP.BRL;
using BYFW.DAL;
using System.Data;
using System.Web.UI.DataVisualization.Charting;

namespace LDSP.UI.EnterpriseAdmin.Report
{
    public partial class CodificationPriorityReport : LDSP.BRL.BaseEnterpriseAdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (this.RB())
                {
                    if (!IsPostBack)
                    {
                        this.InitData();
                    }
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

        public override void RedirectBefore()
        {
            if (LoginUser == null)
            {
                this.GoToLogin();
            }
        }
        protected bool RB()
        {
            return true;
        }

        /// <summary>
        /// 传入参数，设置过滤条件
        /// </summary>
        protected void SetSpecialPageInfo()
        {
        }
        protected void InitData()
        {
            //不缓存则清空页面信息
            if (!needCachePageInfo)
            {
                g_pageInfo.ClearPageInfo();
            }
            this.BindDDLCodification();

            if (IsEnterpriseGroupAdmin)
            {
                this.BindDDLSubdiary();
            }

            this.SetStatus();

            this.SetSpecialPageInfo();
            ShowReport();
        }

        private void SetStatus()
        {
            this.tr_subdiary.Visible = IsEnterpriseGroupAdmin;
        }
        protected void BindDDLCodification()
        {
            this.ddl_codification.Items.Clear();
            this.ddl_codification.Items.Add("法律法规");
            this.ddl_codification.Items.Add("标准");
            this.ddl_codification.SelectedIndex = 0;
        }
        protected void BindDDLSubdiary()
        {
            CommonControlInitial.InitialSubdiary(this.ddl_subdiary, this.LoginUser.Enterprise.GetSubsidiaryList());
        }

        protected string GetCondition(DocType doc)
        {
            string cond = "";
            int docType = 0;
            if (ddl_codification.SelectedIndex == 0)
            {
                docType = (int)doc;
            }
            else
            {
                docType = (int)doc;
            }
            cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("t.docType", EDataType.Int, docType.ToString(), "=", false), true);
            if (this.txt_implementStartDT.Text.Trim() != "")
            {
                cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("t.implementDT", EDataType.Date, this.txt_implementStartDT.Text, ">=", false), true);
            }
            if (this.txt_implementEndDT.Text.Trim() != "")
            {
                cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("t.implementDT", EDataType.Date, this.txt_implementEndDT.Text, "<=", false), true);
            }
            return cond;
        }
        private void ShowReport()
        {
            this.CreateReportInfo(cht_codification, DocType.法律法规, "法规优先级分布图");
            this.CreateReportInfo(cht_standard, DocType.标准, "标准优先级分布图");
        }
        protected void CreateReportInfo(Chart chart, DocType type, string title)
        {
            var etts = new List<R_EnterpriseCodification>();

            //如果是集团用户，则搜索所有集团用户或子公司用户
            if (IsEnterpriseGroupAdmin)
            {
                IList<Enterprise> enterprises = new List<Enterprise>();

                if (this.ddl_subdiary.SelectedValue == "0")
                {
                    enterprises = this.LoginUser.Enterprise.GetSubsidiaryList();
                }
                else
                {
                    DataRow enRow = new Enterprise().GetRecordByID(Int32.Parse(this.ddl_subdiary.SelectedValue));
                    if (enRow != null)
                    {
                        Enterprise en = new Enterprise();
                        en.GetProperties(enRow);
                        enterprises.Add(en);
                    }
                }

                foreach (Enterprise en in enterprises)
                {
                    List<R_EnterpriseCodification> tmp = new R_EnterpriseCodification().GetReportForPriority(en.ID, this.GetCondition(type));
                    foreach (R_EnterpriseCodification reg in tmp)
                    {
                        etts.Add(reg);
                    }
                }
            }
            else
            {
                etts = new R_EnterpriseCodification().GetReportForPriority(LoginUser.EnterpriseID, this.GetCondition(type));
            }

            int low = 0;
            int mid = 0;
            int high = 0;
            foreach (var ett in etts)
            {
                switch (ett.Priority)
                {
                    case Priority.低:
                        low++;
                        break;
                    case Priority.中:
                        mid++;
                        break;
                    case Priority.高:
                        high++;
                        break;
                }
            }
            UIHelper.SetCommonChart(chart, title);

            double[] yValues = { low, mid, high };
            string[] xValues = { "低", "中", "高" };
            chart.Series[0].Points.DataBindXY(xValues, yValues);
        }

        protected void btn_search_Click(object sender, EventArgs e)
        {
            try
            {
                ShowReport();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
    }
}